extern crate fejpsl_contract;
use fejpsl_contract::rank::Fan;
use self::fejpsl_contract::rank::{FanPageData, FanListData, LevelScorePageData, LevelScoreListData, LevelScore};

pub async fn get_rank_fan_list_by_page_no(page_no:&i32) -> FanPageData {
    let client = super::db::connect().await.unwrap();
    let mut vec_fan = Vec::new();
    let per_page_num:i64=10;
    let offset:i64;
    offset= *page_no as i64 *per_page_num;
    let rows = &client.query("select * from (select b.user_uuid,b.nice_name,b.real_name,b.is_explicit,COALESCE(a.fan_num,0) as fan_num,
    b.avatar_text from (select follow_uuid,count(*) fan_num from t_follow_me
    group by follow_uuid order by fan_num desc) a right join t_user b on a.follow_uuid=b.user_uuid
    ) a where user_uuid is not null order by fan_num desc limit $1 offset $2",
                             &[&per_page_num,&offset]).await.unwrap();
    for row in rows {
        let mut m_real_name=String::from("");
        let tmp_str: Option<String> = row.get(2);
        if let Some(_i) = tmp_str {m_real_name= row.get(2); }else{ m_real_name=String::from("") }
        let mut m_avatar_text=String::from("");
        let tmp_str: Option<String> = row.get(5);
        if let Some(_i) = tmp_str {m_avatar_text= row.get(5); }else{ m_avatar_text=String::from("") }
        let fan = Fan {
            user_uuid:                 row.get(0),
            nice_name:          row.get(1),
            real_name:    m_real_name,
            is_explicit:        row.get(3),
            fan_num:            row.get(4),
            avatar_text:        m_avatar_text,
        };

        vec_fan.push(fan);
    }

    let total=rows.len() as i32;
    let page_count=(total/10) as i32;
    let mut over=false;
    if page_no>=&page_count {over=true}
    let size=per_page_num as i32;
    let rank_list_data= FanListData {
        cur_page:*page_no,
        datas:Some(vec_fan),
        offset: offset,
        over: over,
        page_count:page_count,
        size:size,
        total:total,
    };
    let rank_page_data= FanPageData {
        data:rank_list_data,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    return rank_page_data;
}
pub async fn get_rank_friend_level_score_list_by_page_no(page_no:&i32,user_uuid:&String) -> LevelScorePageData {
    let client = super::db::connect().await.unwrap();
    let mut vec_rank = Vec::new();
    let per_page_num:i64=10;
    let offset:i64;
    offset= *page_no as i64 *per_page_num;
    let rows = &client.query("select b.user_uuid,b.nice_name,b.real_name,b.is_explicit,COALESCE(level_score,0) as level_score,
        b.avatar_text from t_follow_me a,t_user b where a.user_uuid=$3
	    and a.is_block=false and a.follow_uuid=b.user_uuid order by level_score desc limit $1 offset $2",
                             &[&per_page_num,&offset,&user_uuid]).await.unwrap();
    for row in rows {
        let mut m_real_name=String::from("");
        let tmp_str: Option<String> = row.get(2);
        if let Some(_i) = tmp_str {m_real_name= row.get(2); }else{ m_real_name=String::from("") }
        let mut m_avatar_text=String::from("");
        let tmp_str: Option<String> = row.get(5);
        if let Some(_i) = tmp_str {m_avatar_text= row.get(5); }else{ m_avatar_text=String::from("") }
        let rank = LevelScore {
            user_uuid:                 row.get(0),
            nice_name:          row.get(1),
            real_name:    m_real_name,
            is_explicit:        row.get(3),
            level_score:            row.get(4),
            avatar_text:        m_avatar_text,
        };

        vec_rank.push(rank);
    }

    let total=rows.len() as i32;
    let page_count=(total/10) as i32;
    let mut over=false;
    if page_no>=&page_count {over=true}
    let size=per_page_num as i32;
    let rank_list_data= LevelScoreListData {
        cur_page:*page_no,
        datas:Some(vec_rank),
        offset: offset,
        over: over,
        page_count:page_count,
        size:size,
        total:total,
    };
    let rank_page_data= LevelScorePageData {
        data:rank_list_data,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    return rank_page_data;
}
pub async fn get_rank_world_level_score_list_by_page_no(page_no:&i32) -> LevelScorePageData {
    let client = super::db::connect().await.unwrap();
    let mut vec_rank = Vec::new();
    let per_page_num:i64=10;
    let offset:i64;
    offset= *page_no as i64 *per_page_num;
    let rows = &client.query("SELECT user_uuid,nice_name,real_name,is_explicit,COALESCE(level_score,0) as level_score, avatar_text
	FROM public.t_user order by level_score desc limit $1 offset $2",
                             &[&per_page_num,&offset]).await.unwrap();
    for row in rows {
        let mut m_real_name=String::from("");
        let tmp_str: Option<String> = row.get(2);
        if let Some(_i) = tmp_str {m_real_name= row.get(2); }else{ m_real_name=String::from("") }
        let mut m_avatar_text=String::from("");
        let tmp_str: Option<String> = row.get(5);
        if let Some(_i) = tmp_str {m_avatar_text= row.get(5); }else{ m_avatar_text=String::from("") }
        let rank = LevelScore {
            user_uuid:                 row.get(0),
            nice_name:          row.get(1),
            real_name:    m_real_name,
            is_explicit:        row.get(3),
            level_score:            row.get(4),
            avatar_text:        m_avatar_text,
        };

        vec_rank.push(rank);
    }

    let total=rows.len() as i32;
    let page_count=(total/10) as i32;
    let mut over=false;
    if page_no>=&page_count {over=true}
    let size=per_page_num as i32;
    let rank_list_data= LevelScoreListData {
        cur_page:*page_no,
        datas:Some(vec_rank),
        offset: offset,
        over: over,
        page_count:page_count,
        size:size,
        total:total,
    };
    let rank_page_data= LevelScorePageData {
        data:rank_list_data,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    return rank_page_data;
}
